<?php


//	define("CMS_QUERY_DEBUG",true);
/**
 * this is BLOB 2.0.. may not work with older originate versions.!!!!
 * 
 */

	include_once("fta.201003310000/fta.php");
	fta_registerModules(
		array(
		"cms.201003310000/config/cmsConfig",//we will deprecate the old cms config soon..
//		"fta/config"
		)
	);
	
	//turn off compression
	ini_set('memory_limit','256M');
	ini_set('zlib.output_compression', '0');
	ini_set('zlib.output_compression_level', '-1');
	ini_set('zlib.output_handler', '');

//	include_once("cmsConfig.php");
	include_once(CMS_HOME_DIR . "/lib/cmsBlobHelper.php");	

	session_start();

	$documentCode= extractNumberWithKeyFromArray( "documentCode", $_REQUEST);

	//backwards compat hack.. we want to use documentCode for every thing, but there are somethings that use document
	//when in this mode.. so id we have no documentCode, look for a document.
	if(is_null($documentCode))
		$documentCode= extractNumberWithKeyFromArray( "document", $_REQUEST);//fucking POS!!//cant remove this.. which is a buggar

	$fileId= extractNumberWithKeyFromArray( "fileId", $_REQUEST);
	$fileTypeCode= extractNumberWithKeyFromArray( "filetypecode", $_REQUEST);

	$elementId = extractNumberWithKeyFromArray("elementId",$_REQUEST);
	$scale = extractBooleanWithKeyFromArray( "scale" , $_REQUEST, false);
	$width = extractStringWithKeyFromArray("width", $_REQUEST ,null);//dunlike this.. 
	$height = extractStringWithKeyFromArray("height", $_REQUEST ,null);
	$grabScreen = extractStringWithKeyFromArray("grabScreen", $_REQUEST ,false);

	$attach= extractBooleanWithKeyFromArray( "attach", $_REQUEST);
	
	$cmsDocument = new CmsDocument($db);


	$responseHeaders = null;
	$data = null;

	$authInfo = extractObjectWithKeyFromArray( "authInfo", $_SESSION );
	if ( $authInfo == null ) {
		$authInfo = setupPublicUser();
		header("X-BLOB-AUTH: public");
		
		//if ($fileTypeCode == 1 || is_null($fileTypeCode))
		//	die("Forbidden");
			
	} else { 
		header("X-BLOB-AUTH: private");	
	}
	
	$blob = null;
	$cachedImage = false;

	//i think that this will be made so it can only be accessed from within the cms ..
	$options = array();
	if ($scale) {
		$options['scale'] = $scale;
		$options['width'] = $width;
		$options['height'] = $height;
	}
		
	if ( isset($documentCode) ) {
	
		//we need to get 
		if (!is_null($elementId)) {
			$options['elementId'] = $elementId;
		}
		
		if (!is_null($fileTypeCode)){
			$options['fileType'] = $fileTypeCode;			
		}
		if (!is_null($fileId)){
			$options['fileId'] = $fileId;
		}
		$options['grabScreen'] = $grabScreen;

		$blob = blobWithDocumentCode($documentCode, $options, $db);					
		
	}
	
	if (!is_null($blob) && is_array($blob) && key_exists('path', $blob) && key_exists('headers', $blob)){
		
		if ($attach){
			sendDoNotCacheHeaders();
			if ($blob['headers']['X-FILENAME']) {
				$blob['headers']['Content-Disposition'] = 'attach; filename="' . $blob['headers']['X-FILENAME'] .'";';
				$blob['headers']['Content-Type'] ="application/x-download";
			}
		} else {
			sendCacheHeaders();
		}
		
		//streamBlobDataWithHeaders($blob['data'], $blob['headers']);
		//echo 'hello world';
		if (is_array($blob['headers'])) {
			foreach ($blob['headers'] as $header => $headerValue){
				header($header.": ". $headerValue, true);				
			}
		}
		
		if ($blob['headers']['Content-length'] >  (1024*500)){
			streamFileWithOutUsingLotsOfRam($blob['path']);	
		} else {
			readfile($blob['path']);
		}
		die;
	} else {
		//otherwise 
		header("HTTP/1.1 404 Not Found");
		header("Content-Type: text/plain");
		echo "what you are looking for,\nis no more.\nhttp error 404\n";
		
		if (is_array($responseHeaders)) {
			echo "Additonal Information:\n";
			foreach ($responseHeaders as $header) {
				echo($header);
			}
		}
		echo "\n";	
	}
	
	
	
?>
